import os
import sys

from core.result_base import ResultBase
from api.favorite_dsh import favorite_dsh
from common.logger import logger
from common.user_agent import useragent
import urllib3
urllib3.disable_warnings()

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(BASE_PATH)


def favorite_dsh_create(access_token,name,parent_id):
    """
    创建收藏夹
    :param name: 收藏夹名称
    :param parent_id : 父文件夹，为空代表是根目录，不为空则代表有父目录
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "name": name,
        "parent_id": parent_id
    }

    headers = {
        "Content-Type": "application/json;charset=utf-8",
        "User-Agent": useragent.get_user_agent_one()
   }

    cookies = {
        "access_token":access_token
    }


    res = favorite_dsh.create(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("创建收藏夹 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def favorite_dsh_delete(access_token,favorite_project_id):
    """
    删除收藏夹
    :param favorite_project_id : 父文件夹，为空代表是根目录，不为空则代表有父目录
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "favorite_project_id": favorite_project_id
    }

    headers = {
        "Content-Type": "application/json;charset=utf-8",
        "User-Agent": useragent.get_user_agent_one()
   }

    cookies = {
        "access_token":access_token
    }


    res = favorite_dsh.delete(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("删除收藏夹 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result

def get_tree(access_token):
  """
    自动获取收藏文件夹列表
    备注：每一次打开仪表盘界面即调用一次
    :return: 自定义的关键字返回结果 result
    """
  result = ResultBase()

  headers = {

    "User-Agent": useragent.get_user_agent_one()

  }

  cookies = {
    "access_token": access_token
  }

  res = favorite_dsh.get_tree_api(headers=headers, cookies=cookies)
  result.success = False

  if res.json()["status"] == "0":
    result.success = True
  else:
    result.error = "接口返回码是 【 {} 】, 返回信息：{}，trcid：{} ".format(res.json()["errstr"], res.json(), res.json('trcid'))
  result.payload = res.json()
  result.response = res
  logger.info("获取收藏仪表盘树 ==>> 返回结果 ==>> {}".format(result.response.text))

  return result